package com.ruoyi.repay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.repay.domain.RepaymentDetails;
import com.ruoyi.repay.domain.RepaymentPlan;
import com.ruoyi.repay.domain.vo.ResultVo;
import com.ruoyi.repay.domain.vo.SearchVo;
import com.ruoyi.repay.mapper.RepaymentDetailsMapper;
import com.ruoyi.repay.service.RepaymentDetailsService;
import com.ruoyi.repay.utils.IdWorker;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.attribute.standard.PrinterURI;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.Period;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RepaymentDetailsImpl implements RepaymentDetailsService {
    @Autowired
    private RepaymentDetailsMapper repaymentDetailsMapper;

    @Override
    public ResultVo repaymentDetailsList(SearchVo  searchVo,String checkCode) {
        QueryWrapper<RepaymentDetails> qw = new QueryWrapper<>();
        qw.eq("check_code", checkCode);
        Page<RepaymentDetails> page = new Page<>(searchVo.getPageNum(),searchVo.getPageSize());
        Page<RepaymentDetails> p = repaymentDetailsMapper.selectPage(page, qw);
//        把数据给到records
        p.setRecords(p.getRecords());
        Map map = new HashMap<>();
        map.put("total",p.getTotal());
        map.put("data",p.getRecords());
//        List<RepaymentDetails> repaymentDetails = repaymentDetailsMapper.repaymentDetailsList(checkCode);
        return new ResultVo(200, "查询成功",map);
    }

    @Override
    public ResultVo add(Integer id) {
        RepaymentDetails repaymentDetails = repaymentDetailsMapper.selectById(id);
        IdWorker idWorker = new IdWorker();
        String outTradeNo = Long.toString(idWorker.nextId());
        repaymentDetails.setOutTradeNo(outTradeNo);
        repaymentDetailsMapper.updateByIds(repaymentDetails);
        return new ResultVo(200, "添加成功", repaymentDetails);
    }

    @Override
    public ResultVo addrepaymentDetails(RepaymentDetails repaymentDetails) {
//        获取前端传过来的总金额和期数
        BigDecimal oldprice = repaymentDetails.getPrice();
        Integer type = repaymentDetails.getType();
//        使用RoundingMode.HALF_UP表示四舍五入
        for (Integer i = 0; i < type; i++) {
//            价格循环添加
            BigDecimal price = oldprice.divide(new BigDecimal(type), 2, BigDecimal.ROUND_HALF_UP);
            repaymentDetails.setPrice(price);
//            每条还款详细信息的期数
           repaymentDetails.setType(i+1);
//           获取当前日期
            LocalDateTime time = repaymentDetails.getTime();
//          每月需要还款的日期，每天记录递增一月
            LocalDateTime plustime = time.plus(Period.ofMonths(1));
            repaymentDetails.setTime(plustime);
//            还款状态默认为1 待还款·
            repaymentDetails.setRepayStatus(1);
            repaymentDetailsMapper.addrepaymentDetails(repaymentDetails);
        }

        return new ResultVo(200, "添加成功", repaymentDetails);
    }
}
